<template>
  <el-dialog title="关联角色" :visible.sync="visible" width="700px" append-to-body :close-on-click-modal="false">
    <el-table ref="RoleTableRef"
              :border="Global.tableShowBorder" v-loading="loading" :data="roleList"
              row-key="roleId"
              @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="角色编号" prop="roleId" width="120"/>
      <el-table-column label="角色名称" prop="roleName" :show-overflow-tooltip="true"/>
      <el-table-column label="权限字符" prop="roleKey" :show-overflow-tooltip="true"/>
      <!--      <el-table-column label="显示顺序" prop="roleSort"/>-->
      <!--      <el-table-column label="状态" align="center">-->
      <!--        <template slot-scope="scope">-->
      <!--          <el-switch v-model="scope.row.status" active-value="0" inactive-value="1" :disabled="true"></el-switch>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <!--      <el-table-column label="创建时间" align="center" prop="createTime">-->
      <!--        <template slot-scope="scope">-->
      <!--          <span>{{ parseTime(scope.row.createTime) }}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">-->
      <!--        <template slot-scope="scope" v-if="scope.row.roleId !== 1">-->
      <!--          <el-button size="mini" type="text" icon="el-icon-edit" @click="allotFolder(scope.row)" v-hasPermi="['am:authRole:add']">授权文件夹</el-button>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit">保 存</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {listRole, getRole, delRole, addRole, updateRole, dataScope, changeRoleStatus} from "@/api/system/role";
import {getRoleIdsByDirId, saveDirectoryRole} from "../../../api/am/directory";

export default {
  name: 'ModalLinkRole',
  data() {
    return {
      visible: false,
      loading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 角色表格数据
      roleList: [],
      // 日期范围
      dateRange: [],
      // 数据范围选项
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        roleName: undefined,
        roleKey: undefined,
        status: undefined
      },
      directory: {
        id: -1
      },
      selectedRoleIds: [],
      multipleSelection: []
    }
  },
  methods: {
    open(row) {
      this.directory = {...row}
      this.visible = true;
      this.getDirLinkedRoleIds();
    },
    getDirLinkedRoleIds() {
      console.log(this.directory.id);
      getRoleIdsByDirId(this.directory.id).then(response => {
        console.log(response);
        this.selectedRoleIds = [...response.data]
        this.getList()
      });
    },
    /** 查询角色列表 */
    getList() {
      this.loading = true;
      listRole(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.roleList = response.rows;
          this.total = response.total;
          this.loading = false;
          this.$nextTick(() => {
            this.roleList.forEach(role => {
              if (this.selectedRoleIds.includes(role.roleId)) {
                console.log(123);
                this.$refs.RoleTableRef.toggleRowSelection(role)
              }
            })
          })
        }
      );
    },
    handleSelectionChange(val) {
      console.log(val);
      this.multipleSelection = val;
      // this.multipleSelection = val;
    },
    submit() {
      saveDirectoryRole({
        roleIds: this.multipleSelection.map(role => role.roleId),
        directoryId: this.directory.id,
      }).then(response => {
        console.log(response);
        this.$modal.msgSuccess("关联角色成功");
      })
    },
    cancel() {
      this.visible = false;
    },
  }
}
</script>

<style lang="less"></style>
